package com.dc.seckill.controller;

import com.dc.seckill.domain.MiaoshaUser;
import com.dc.seckill.redis.GoodsKey;
import com.dc.seckill.redis.RedisService;
import com.dc.seckill.service.GoodsService;
import com.dc.seckill.service.MiaoshaUserService;
import com.dc.seckill.vo.GoodsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @模块 商品模块
 * @作者 djs
 * @时间 2018/3/20 11:32
 * @版本 V1.0
 * @描述 对商品进行相关操作的控制层
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    private Logger log = LoggerFactory.getLogger(GoodsController.class);

    @Autowired
    MiaoshaUserService userService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    RedisService redisService;

    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;

    @Autowired
    ApplicationContext applicationContext;

    /**
     * 获取秒杀商品列表
     * @params [reponse, model, user]
     * @returns java.lang.String
     */
    @RequestMapping(value = "/to_list",produces = "text/html")
    @ResponseBody
    public String list(HttpServletRequest request, HttpServletResponse response,
                       Model model, MiaoshaUser user){

        log.info("开始请求秒杀商品列表");
        //1:如果页面有缓存，直接返回缓存
        String html = redisService.get(GoodsKey.goods_list,"",String.class);
        if(!StringUtils.isEmpty(html)){
            return html;
        }
        //2:获取秒杀商品列表
        List<GoodsVo> goodsList = goodsService.listGoodsVo();

        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsList);

        //3:渲染页面，存入缓存
        if(StringUtils.isEmpty(html)){
            SpringWebContext context = new SpringWebContext(request,response,
                    request.getServletContext(),request.getLocale(),model.asMap(),applicationContext);
            html = thymeleafViewResolver.getTemplateEngine().process("goods_list",context);
            redisService.set(GoodsKey.goods_list,"",html);
        }

        return html;
    }

    /**
     * 获取商品详情
     * @params [model, user, goodsId]
     * @returns java.lang.String
     */
    @RequestMapping(value = "/to_detail/{goodsId}",produces = "text/html")
    @ResponseBody
    public String detail(HttpServletRequest request, HttpServletResponse response,
                         Model model, MiaoshaUser user, @PathVariable("goodsId") Long goodsId){

        log.info("根据商品ID【"+goodsId+"】请求商品详情");

        //1:如果有页面缓存，直接返回缓存
        String html = redisService.get(GoodsKey.goods_detail,""+goodsId,String.class);
        if(!StringUtils.isEmpty(html)){
            return html;
        }

        //2:根据商品ID获取秒杀商品信息
        GoodsVo goods = goodsService.queryGoodsVoByGoodsID(goodsId);

        //3:判断秒杀状态
        long startTime = goods.getStartDate().getTime();    //活动开始时间
        long endTime = goods.getEndDate().getTime();    //活动结束时间
        long now = System.currentTimeMillis();  //当前时间
        int countDown = 0;
        int miaoshaStatus = 2;

        if(now < startTime ){ //活动还没开始
            miaoshaStatus = 0;
            countDown = (int) (startTime-now)/1000;
        }else if(endTime < now){    //活动已经结束
            miaoshaStatus = 2;
            countDown = -1;
        }else if(startTime < now && now <endTime){  //活动进行中
            miaoshaStatus = 1;
            countDown = 0;
        }

        model.addAttribute("user",user);
        model.addAttribute("goods",goods);
        model.addAttribute("miaoshaStatus",miaoshaStatus);
        model.addAttribute("countDown",countDown);

        //4:渲染页面，存入缓存
        if(StringUtils.isEmpty(html)){
            SpringWebContext context = new SpringWebContext(request,response,
                    request.getServletContext(),request.getLocale(),model.asMap(),applicationContext);
            html = thymeleafViewResolver.getTemplateEngine().process("goods_detail",context);
            redisService.set(GoodsKey.goods_detail,""+goodsId,html);
        }

        return html;
    }

}
